﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.ServiceModel;
using FrameDisplayServer.Services;
using System.ServiceModel.Description;
using System.Threading;
using FrameDisplayServer.Browser;

using FrameDisplay.Services;
using System.IO;
using FrameDisplayServer.Services.WebInterface;


namespace FrameDisplayServer
{
    public partial class FrameDisplayServerForm : Form
    {      

        public FrameDisplayServerForm()
        {
            InitializeComponent();

            string xulRunnerPath = @"C:\xulrunner";
            if (File.Exists(xulRunnerPath + @"\xpcom.dll"))
            {
                try
                {
                    Skybound.Gecko.Xpcom.Initialize(xulRunnerPath);
                    FrameDisplayServer.Browser.MozillaBrowserForm.NotifyXpcomInitialized();
                }
                catch
                {
                }
            }            
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);            

            AppCentralConfiguration config = new AppCentralConfiguration();
            config.MainForm = this;
            config.ServiceManager = new ServiceManager();
            config.SettingsManager = new SettingsManager();
            config.BrowserManager = new BrowserManager();
            AppCentral.Initialize(config);

            AppCentral.ServiceManager.StartServices();

            this.PopulateInfo();
        }

        protected override void OnClosed(EventArgs e)
        {
            base.OnClosed(e);

            if (AppCentral.IsInitialized)
            {
                if (AppCentral.BrowserManager is IDisposable)
                {
                    ((IDisposable)AppCentral.BrowserManager).Dispose();
                }
            }

            AppCentral.ServiceManager.StopServices();
        }


        private void PopulateInfo()
        {
            this.textBoxServerUrl.Text = AppCentral.ServiceManager.BaseUrl;

            if (AppCentral.IsInitialized)
            {
                this.linkLabelAccessCode.Text = AppCentral.SettingsManager.AccessCode;

                string targetScreen = AppCentral.SettingsManager.BrowserTargetScreenDeviceName;
                if (String.IsNullOrEmpty(targetScreen))
                {
                    this.linkLabelPrimaryScreen.Text = Screen.PrimaryScreen.DeviceName;
                }
                else
                {
                    this.linkLabelPrimaryScreen.Text = targetScreen;
                }

                if (AppCentral.SettingsManager.BrowserType == "Mozilla")
                {
                    this.linkLabelBrowserType.Text = "Firefox";
                }
                else
                {
                    this.linkLabelBrowserType.Text = "Internet Explorer";
                }
            }
        }
       

        private void linkLabelPrimaryScreen_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            FrameDisplayServer.UI.Settings.PickScreenForm pickScreenForm = new FrameDisplayServer.UI.Settings.PickScreenForm();
            pickScreenForm.ScreenName = AppCentral.SettingsManager.BrowserTargetScreenDeviceName;
            if (pickScreenForm.ShowDialog(this) == DialogResult.OK)
            {
                AppCentral.SettingsManager.BrowserTargetScreenDeviceName = pickScreenForm.ScreenName;
                this.PopulateInfo();
            }
        }

        private void linkLabelBrowserType_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            FrameDisplayServer.UI.Settings.PickBrowserTypeForm pickBrowserTypeForm = new FrameDisplayServer.UI.Settings.PickBrowserTypeForm();
            pickBrowserTypeForm.ShowDialog(this);            
            this.PopulateInfo();            
        }

        private void linkLabelAccessCode_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            FrameDisplayServer.UI.Settings.PickAccessCodeForm pickAccessCodeForm = new FrameDisplayServer.UI.Settings.PickAccessCodeForm();
            if (pickAccessCodeForm.ShowDialog(this) == DialogResult.OK)
            {
                this.PopulateInfo();
            }
        }
       
    }
}
